package com.aida.evil.eda.WebView;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.aida.evil.aida.R;
import com.aida.evil.eda.Data.NetworkData;

/********************************************
 * 工程名：Aida
 * 包名：com.aida.evil.eda.WebView
 * 时间：16-5-8 上午11:35
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：浏览器页面
 ********************************************/
public class WebViewActivity extends AppCompatActivity
{
    private String url;
    private ProgressBar progressBar;
    private Toolbar toolbar;
    private WebView webView;

    public WebViewActivity()
    {
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        initView();
        getIntentData();
        setToolBar();
        setWebView();
    }

    /**
     * 获取intent数据
     */
    private void getIntentData()
    {
        String url = getIntent().getStringExtra("url");
        this.url = "http://" + NetworkData.ServiceIP + "/details/" + url;
        Log.i("url",this.url);
        //this.url="http://www.baidu.com";
    }

    /**
     * 设置ToolBar
     */
    private void setToolBar()
    {
        toolbar.setTitle("详情页");
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    /**
     * toolbar返回键监听
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if (item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * WebView设置
     */
    private void setWebView()
    {
        WebSettings webSettings = webView.getSettings();

        /*设置适应屏幕*/
        {
            webSettings.setUseWideViewPort(true);//设置此属性，可任意比例缩放
            webSettings.setLoadWithOverviewMode(true);
        }
        /*支持缩放*/
        {
            webSettings.setJavaScriptEnabled(true);
            webSettings.setBuiltInZoomControls(true);
            webSettings.setSupportZoom(true);
        }
        /*支持手动输入*/
        {
            //webView.requestFocusFromTouch();
        }

        /*优先使用缓存*/
        {
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            // 开启 DOM storage API 功能
            webSettings.setDomStorageEnabled(true);
        }


        webView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress)
            {
                if (newProgress == 100)
                {
                    progressBar.setVisibility(View.GONE);
                }
                else
                {
                    progressBar.setProgress(newProgress);
                }
            }
        });

        /*在当前页面打开新的链接*/
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                progressBar.setVisibility(View.VISIBLE);
                return true;
            }
        });
    }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack())
        {
            webView.goBack();
            return true;
        }
        else
        {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 初始化控件
     */
    private void initView()
    {
        progressBar = (ProgressBar) findViewById(R.id.webview_progressBar);
        toolbar = (Toolbar) findViewById(R.id.answer_toolbar);
        webView = (WebView) findViewById(R.id.webView);
    }
}
